/* $Id: OleDBServerConnector.cs,v 1.4 2004/11/24 13:57:05 larsbm Exp $
 * Copyright (c) 2004 Engine EAR GmbH & Co. KG
 * Developed by: Lars Behrmann, lb@engine.de
 */

using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using SQLToNeo.PlugIn;

namespace SQLToNeo.Model
{

	public class OleDBServerConnector : IDatabaseConnector
	{
		private string _connectionNT = "Provider=SQLOLEDB;Data Source=#source#;Integrated Security=SSPI";
		private string _connectionUser = "Provider=SQLOLEDB;Data Source=#source#;User Id=#user#;Password=#password#";
		private string _connection;

		public OleDBServerConnector()
		{}

		#region IDatabaseConnector Member

		public bool CreateConnectionString(string db, string user, string password, bool ntauthentication)
		{
			if(db.Length == 0)
				return false;

			if(ntauthentication)
			{				
				_connectionNT = _connectionNT.Replace("#source#", db);
				_connection = _connectionNT;
				return true;
			}
			else
			{
				if(user.Length == 0 || password.Length == 0)
					return false;
				else
				{
					_connectionUser = _connectionUser.Replace("#source#", db);
					_connectionUser = _connectionUser.Replace("#user#", user);
					_connectionUser = _connectionUser.Replace("#password#", password);
					_connection = _connectionUser;
					return true;
				}
			}
		}

		public ArrayList GetExistingDatabaseNames()
		{
			ArrayList al = new ArrayList();
			DataTable dt = new DataTable();
			OleDbConnection conn = new OleDbConnection(ConnectionString);
			try
			{
				conn.Open();
				dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Catalogs, new object[] {});
			}
			catch(OleDbException e)
			{
				MessageBox.Show("Error while resolving existing databases!\nError:\n"+e.Message.ToString());
			}
			conn.Close();
			foreach(DataRow dr in dt.Rows)
				al.Add(dr["CATALOG_NAME"].ToString());
			return al;
		}

		public string ConnectionString
		{
			get
			{
				return _connection;
			}
			set
			{
				_connection = value;
			}
		}

		public bool NTAuthenticationPossible
		{
			get { return true; }
		}
		#endregion
	}
}

/*
 * $Log: OleDBServerConnector.cs,v $
 * Revision 1.4  2004/11/24 13:57:05  larsbm
 * - new project SQLToNeoPlugIn
 * - switched interfaces from mainsource into plug in
 *
 * Revision 1.3  2004/11/02 14:02:38  larsbm
 * - Add new property to IDataBaseConnector
 * - Form for displaying possible conflicts
 * - Implement Conflictform
 * - Add Mainmenu to mainform
 *
 * Revision 1.2  2004/10/14 09:12:17  larsbm
 * - Start to implement anaylzer details
 * - Add tests for that
 *
 */
